package team.knowers.qzh.qzhserver.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import team.knowers.qzh.qzhserver.dao.entity.CourseDO;
import team.knowers.qzh.qzhserver.dao.entity.CourseEvaluationDO;
import team.knowers.qzh.qzhserver.dao.entity.SubjectDO;
import team.knowers.qzh.qzhserver.dao.mapper.SubjectMapper;
import team.knowers.qzh.qzhserver.dto.req.PageSubjectCourseReqDTO;
import team.knowers.qzh.qzhserver.dto.resp.CourseSimpleRespDTO;
import team.knowers.qzh.qzhserver.dto.resp.EvaluationPageRespDTO;
import team.knowers.qzh.qzhserver.dto.resp.ListSubjectsRespDTO;
import team.knowers.qzh.qzhserver.dto.resp.SubjectDetailsRespDTO;
import team.knowers.qzh.qzhserver.service.CourseService;
import team.knowers.qzh.qzhserver.service.SubjectService;

import java.util.ArrayList;
import java.util.List;

/**
 * 专业业务层
 *
 * @author xtg
 */
@Service
@RequiredArgsConstructor
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, SubjectDO> implements SubjectService {
    private final CourseService courseService;

    @Override
    public List<ListSubjectsRespDTO> listSubjects() {
        LambdaQueryWrapper<SubjectDO> queryWrapper = Wrappers.lambdaQuery(SubjectDO.class).eq(SubjectDO::getDelFlag, 0);
        List<SubjectDO> subjectDOS = baseMapper.selectList(queryWrapper);
        List<ListSubjectsRespDTO> results = new ArrayList<>();
        for (SubjectDO subjectDO : subjectDOS) {
            results.add(BeanUtil.toBean(subjectDO, ListSubjectsRespDTO.class));
        }
        return results;
    }

    @Override
    public SubjectDetailsRespDTO subjectDetails(Long subjectId) {
        LambdaQueryWrapper<SubjectDO> queryWrapper = Wrappers.lambdaQuery(SubjectDO.class).eq(SubjectDO::getDelFlag, 0).eq(SubjectDO::getId, subjectId);
        SubjectDO subjectDO = baseMapper.selectOne(queryWrapper);
        return BeanUtil.toBean(subjectDO, SubjectDetailsRespDTO.class);
    }

    @Override
    public IPage<CourseSimpleRespDTO> pageSubjectCourseByTimeDesc(PageSubjectCourseReqDTO reqDTO) {
        LambdaQueryWrapper<CourseDO> eq = Wrappers.lambdaQuery(CourseDO.class)
                .eq(CourseDO::getDelFlag, 0)
                .eq(CourseDO::getStatus, 2)
                .eq(CourseDO::getSubjectId, reqDTO.getSubjectId())
                .orderByDesc(CourseDO::getCreateTime);
        IPage<CourseDO> resultPage = courseService.getBaseMapper().selectPage(reqDTO, eq);
        return resultPage.convert(each -> {
            CourseSimpleRespDTO bean = BeanUtil.toBean(each, CourseSimpleRespDTO.class);
            bean.setMark(courseService.getMarkById(bean.getId()));
            return bean;
        });
    }

    @Override
    public IPage<CourseSimpleRespDTO> pageSubjectCourseBySalesVolumeDesc(PageSubjectCourseReqDTO reqDTO) {
        LambdaQueryWrapper<CourseDO> eq = Wrappers.lambdaQuery(CourseDO.class)
                .eq(CourseDO::getDelFlag, 0)
                .eq(CourseDO::getSubjectId, reqDTO.getSubjectId())
                .eq(CourseDO::getStatus, 2)
                .orderByDesc(CourseDO::getSalesVolume);
        IPage<CourseDO> resultPage = courseService.getBaseMapper().selectPage(reqDTO, eq);
        return resultPage.convert(each -> {
            CourseSimpleRespDTO bean = BeanUtil.toBean(each, CourseSimpleRespDTO.class);
            bean.setMark(courseService.getMarkById(bean.getId()));
            return bean;
        });
    }
}
